<?php
namespace app\client\controller;

use think\Url;
use think\Loader;
use think\Session;
use think\Request;
use think\Controller;
use app\client\model\Coupon as CouponModel;

class Coupon extends Controller
{

    /**
     * 店铺优惠券列表
     * @param Request $request
     * @param CouponModel $coupon
     * @return mixed
     */
    public function index(Request $request, CouponModel $coupon)
    {
        // 获取参数
        $inquire = $request->instance()->param();

        // 搜索条件
        $condition['member_id'] = ['eq', Session::get('shop_id')];


        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];


        $data = $coupon->where($condition)->order(['id' => 'desc'])->paginate(10);


        return $this->fetch('index', [
            'mate_title' => '优惠券列表',
            'data'       => $data
        ]);
    }


    /**
     * 新增优惠券
     * @param Request $request
     * @param CouponModel $coupon
     * @return mixed
     */
    public function create(Request $request, CouponModel $coupon)
    {

        if ($request->instance()->isPost()) {


            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('Coupon');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            if ($data['virtual_price']<$data['actual_price']) fail('满减金额必须大于优惠金额');

            if ($data['virtual_price']==0||$data['actual_price']==0) fail('满减金额和优惠金额必须大于0！！！');

            // 提交数据
            $dispose = $coupon->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('Coupon/index'));
            }

            fail('操作失败');

        }

        return $this->fetch('create', [
            "mate_title" => '新增优惠券',
        ]);

    }


    /**
     * 删除优惠券
     * @param $id
     * @param CouponModel $coupon
     */
    public function destroy($id, CouponModel $coupon)
    {

        if ($coupon::destroy($id)) {
            win('操作成功', Url::build('Coupon/index'));
        }

        fail('操作失败');

    }


    /**
     * 编辑优惠券
     * @param $id
     * @param Request $request
     * @param CouponModel $coupon
     * @return mixed
     */
    public function update($id, Request $request, CouponModel $coupon)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('Coupon');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $coupon->allowField(true)->save($data, ['id' => $id]);

            if ($dispose) {
                win('操作成功', Url::build('Coupon/index'));
            }

            fail('操作失败');

        }

        $item = $coupon::get($id);


        return $this->fetch('create', [
            "mate_title" => '优惠券编辑',
            'item'       => $item

        ]);
    }


    /**
     * 更改状态
     * @param Request $request
     * @param CouponModel $coupon
     * @return bool|int
     */
    public function auditing(Request $request, CouponModel $coupon)
    {
        $id = $request->instance()->param('id');

        $status = $coupon->where(['id' => $id])->value('status') == '1' ? 2 : 1;

        if ($coupon->where(['id' => $id])->update(['status' => $status])) {

            return $status;
        }
        return false;
    }


    /**
     * 排列顺序
     * @param Request $request
     * @param CouponModel $coupon
     */
    public function sorting(Request $request, CouponModel $coupon)
    {

        $id = $request->instance()->param('orderId/a');
        $sort = $request->instance()->param('sort/a');

        foreach ($id as $key => $value) {
            $coupon->where('id', $value)->update(['sort' => $sort[$key]]);
        }

        win('操作成功', Url::build('Coupon/index'));
    }
}
